
package org.bridgen.lang.semantics.ast;
import java.util.*;


public class AttrUse extends Use implements Cloneable {
    public void flushCache() {
        super.flushCache();
    }
     @SuppressWarnings({"unchecked", "cast"})  public AttrUse clone() throws CloneNotSupportedException {
        AttrUse node = (AttrUse)super.clone();
        node.in$Circle(false);
        node.is$Final(false);
    return node;
    }
     @SuppressWarnings({"unchecked", "cast"})  public AttrUse copy() {
      try {
          AttrUse node = (AttrUse)clone();
          if(children != null) node.children = (ASTNode[])children.clone();
          return node;
      } catch (CloneNotSupportedException e) {
      }
      System.err.println("Error: Could not clone node of type " + getClass().getName() + "!");
      return null;
    }
     @SuppressWarnings({"unchecked", "cast"})  public AttrUse fullCopy() {
        AttrUse res = (AttrUse)copy();
        for(int i = 0; i < getNumChildNoTransform(); i++) {
          ASTNode node = getChildNoTransform(i);
          if(node != null) node = node.fullCopy();
          res.setChild(node, i);
        }
        return res;
    }
    // Declared in NameAnalysis.jrag at line 61

	
	/** 
	 * Checking:
	 * 1) Must have a corresponding attribute declaration
	 */
	public void nameAnalysis(Collection errorList) {
		if (decl() == null) {
			error(errorList, "Attribute " + getAttrName() + " is undeclared");
		}
	}

    // Declared in PrettyPrint.jrag at line 271

    
    public void pp(StringBuffer buf) {
    	getUse().pp(buf);
    	buf.append("." + getAttrName());
    }

    // Declared in TypeCodeGen.jrag at line 527

	
	public void generateCode(String indent, StringBuffer buf) {
		getUse().generateCode(indent, buf);
		buf.append("." + getAttrName() + "()");
	}

    // Declared in TypeCodeGen.jrag at line 541

	
	public void collectAttributeChecks(TreeSet<String> set) {
		if (decl().isNodeAttrDecl()) {
			set.add(callString() + " != null");
		}
		super.collectAttributeChecks(set);
	}

    // Declared in bridgit.ast at line 3
    // Declared in bridgit.ast line 117

    public AttrUse() {
        super();


    }

    // Declared in bridgit.ast at line 10


    // Declared in bridgit.ast line 117
    public AttrUse(Use p0, String p1) {
        setChild(p0, 0);
        setAttrName(p1);
    }

    // Declared in bridgit.ast at line 15


  protected int numChildren() {
    return 1;
  }

    // Declared in bridgit.ast at line 18

  public boolean mayHaveRewrite() { return false; }

    // Declared in bridgit.ast at line 2
    // Declared in bridgit.ast line 117
    public void setUse(Use node) {
        setChild(node, 0);
    }

    // Declared in bridgit.ast at line 5

    public Use getUse() {
        return (Use)getChild(0);
    }

    // Declared in bridgit.ast at line 9


    public Use getUseNoTransform() {
        return (Use)getChildNoTransform(0);
    }

    // Declared in bridgit.ast at line 2
    // Declared in bridgit.ast line 117
    protected String tokenString_AttrName;

    // Declared in bridgit.ast at line 3

    public void setAttrName(String value) {
        tokenString_AttrName = value;
    }

    // Declared in bridgit.ast at line 6

    public String getAttrName() {
        return tokenString_AttrName != null ? tokenString_AttrName : "";
    }

    // Declared in NameAnalysis.jrag at line 106
 @SuppressWarnings({"unchecked", "cast"})     public AttrDecl decl() {
        AttrDecl decl_value = decl_compute();
        return decl_value;
    }

    private AttrDecl decl_compute() {  return lookupAttrDecl(getAttrName());  }

    // Declared in NameAnalysis.jrag at line 150
 @SuppressWarnings({"unchecked", "cast"})     public AttrDecl lookupAttrDecl(String name) {
        AttrDecl lookupAttrDecl_String_value = lookupAttrDecl_compute(name);
        return lookupAttrDecl_String_value;
    }

    private AttrDecl lookupAttrDecl_compute(String name) {
		Use u = getUse();
		if (u.isPatternUse()) {
			return ((PatternUse)u).lookupAttrDecl(getAttrName());	
		} else if (u.isAttrUse()) {
			AttrDecl attrDecl = ((AttrUse)u).decl();
			if (attrDecl == null) {
				return null;
			}
			if (!attrDecl.isNodeAttr()) {
				return null;
			}
			NodeAttrDecl nodeAttrDecl = (NodeAttrDecl)attrDecl;
			NodeDecl returnNodeDecl = nodeAttrDecl.getReturnType().decl();
			if (returnNodeDecl == null) {
				return null;
			}
			return returnNodeDecl.lookupAttrDecl(getAttrName());
		}
		return null;		
	}

    // Declared in NameAnalysis.jrag at line 184
 @SuppressWarnings({"unchecked", "cast"})     public boolean isAttrUse() {
        boolean isAttrUse_value = isAttrUse_compute();
        return isAttrUse_value;
    }

    private boolean isAttrUse_compute() {  return true;  }

    // Declared in TypeAnalysis.jrag at line 72
 @SuppressWarnings({"unchecked", "cast"})     public String type() {
        String type_value = type_compute();
        return type_value;
    }

    private String type_compute() {  return decl() != null ? decl().returnType() : super.type();  }

    // Declared in TypeCodeGen.jrag at line 554
 @SuppressWarnings({"unchecked", "cast"})     public String callString() {
        String callString_value = callString_compute();
        return callString_value;
    }

    private String callString_compute() {  return getUse().callString() + "." + getAttrName() + "()";  }

public ASTNode rewriteTo() {
    return super.rewriteTo();
}

}
